<?php

namespace app\helps;
use app\facade\Redis;
use think\Db;

class MoneyVolumeHelper extends MoneyHelperBaser{

    public function __construct(){
        $this->userHelper = new UserHelper();
    }

    /**
     * 商城下单（兑换券购买）
     * 此方法 外部必须try 和做 msq 事务，
     * 如果我返回的字段不等于1 ，则必须回滚
     * @param $buyUser 购买用户
     * @param $num 购买商品消耗mt 数量
     * @param $msg 备注信息
     * @return str 如果是1 标识成功；否则 返回错误信息
     */
    public function shopBuyGoods($buyUser,$num,$msg) {
        if ($buyUser["status"]==0){
            return "您的账号被禁用,请联系客户解禁";
        }
        if ($num<=0){
            return "消耗兑换券数量不能小于0";
        }
        $volume =$buyUser[self::FILED_VOLUME];
        if ($volume<$num){
            return "兑换券不足";
        }
        $msgstr ="用户购买商品:".$msg;
        //扣除用户兑换券 $user,$num,$filed,$msgStr,$type,$op_type,$profit_type
        $type = self::TYPE_VOLUME;
        $op_type =self::OP_TYPE_VOLUME_SHOP_BUY;
        $res = $this->reduceUserMoneyAndRecord($buyUser,$num,self::FILED_VOLUME,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        //如果未激活 激活用户
        if ($buyUser["activity"]!=1){
           return  $this->userHelper->userActivity($buyUser,"购买商品【激活】",true);
        }
        return 1;
    }

    /**
     * 兑换券充值
     * @param $user 用户
     * @param $num  数量
     * @param $msg 备注
     */
    public function rechargeVolume($user,$num){
        $msgstr ="兑换券充值:".$num;
        //扣除用户兑换券 $user,$num,$filed,$msgStr,$type,$op_type,$profit_type
        $type = self::TYPE_VOLUME;
        $op_type =self::OP_TYPE_VOLUME_RECHARGE;
        $res = $this->addUserMoneyAndRecord($user,$num,self::FILED_VOLUME,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }



    /**
     * 兑换券转赠
     * @param $user 发放mt用户
     * @param $num  数量
     * @param $msg 备注
     */
    public function transferVolume($user,$toUser,$num){
        $type = self::TYPE_VOLUME;
        $op_type =self::OP_TYPE_VOLUME_TRANSFER_FROM;
        $msg="用户转给:".$toUser["username"]."兑换券".$num;
        $res = $this->reduceUserMoneyAndRecord($user,$num,self::FILED_VOLUME,$msg,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        $type = self::TYPE_VOLUME;
        $op_type =self::OP_TYPE_VOLUME_TRANSFER_TO;
        $msg="来自用户:".$user["username"]."转赠兑换券".$num;
        $res = $this->addUserMoneyAndRecord($toUser,$num,self::FILED_VOLUME,$msg,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }




    /**
     * 兑换券充值
     * @param $user 用户
     * @param $num  数量
     * @param $msg 备注
     */
    public function award($user,$num,$msgstr){
        $type = self::TYPE_VOLUME;
        $op_type =self::OP_TYPE_VOLUME_AWARD;
        $res = $this->addUserMoneyAndRecord($user,$num,self::FILED_VOLUME,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }






}